#include "ICritvsLengthDiffHAlgo.h"

#include "ICritvsLengthAlgo.h"
#include "ICritvsLengthSave.h"
#include "InitialPosition.h"
#include "FinalPosition.h"

#include <QString>
#include <QFile>
#include <QTextStream>

ICritvsLengthDiffHAlgo::ICritvsLengthDiffHAlgo()
{

}

ICritvsLengthDiffHAlgo::~ICritvsLengthDiffHAlgo()
{

}

int ICritvsLengthDiffHAlgo::solve(Clock clock,Output output,Pulse *field,Pulse *current,MultiLayer multiLayer,NewParameters params)
{
  int layerNumber=multiLayer.getSolveLayer();
  for(field->setLevel(params.getField()["min"]);field->getLevel(clock.getTimeStamp())<=params.getField()["max"];field->setLevel(field->getLevel(clock.getTimeStamp())+params.getField()["step"]))
    {  
      QString filename,oldname;
      oldname=output.getFileName();
      multiLayer[layerNumber].setMag(InitialPosition(clock,output,field,current,multiLayer,params));
      //multiLayer[layerNumber].setSwitchedMag(FinalPosition(clock,output,field,current,multiLayer,params));
      //std::cout<<multiLayer[layerNumber].getMag().getZ()<<"=="<<multiLayer[layerNumber].getSwitchedMag().getZ()<<std::endl;
      filename=output.getFileName();
      filename.append("_");
      QString string;
      filename.append(string.setNum(field->getLevel(clock.getTimeStamp())));
      filename.append("_.txt");
      output.initialize(filename);
      output.setSave(false);
      ICritvsLengthSave().initialize(output);
      ICritvsLengthAlgo().solve(clock,output,field,current,multiLayer,params);
      output.initialize(oldname);
    }
  return 0;
}
